<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="Shipment ID" prop="shipmentId">
        <el-input
          v-model="queryParams.shipmentId"
          placeholder="请输入Shipment ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="账号标记" prop="account">
        <el-input
          v-model="queryParams.account"
          placeholder="请输入账号标记"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['logistics:logisticsTracking:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['logistics:logisticsTracking:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['logistics:logisticsTracking:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['logistics:logisticsTracking:export']"
          >导出</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['system:tracking:import']"
          >导入</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="logisticsTrackingList"
      @selection-change="handleSelectionChange"
      max-height="680px"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="Shipment ID" align="center" prop="shipmentId" show-overflow-tooltip/>
      <el-table-column label="账号标记" align="center" prop="account" />
      <el-table-column
        label="发货日期"
        align="center"
        prop="sendDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sendDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="体积重除数" align="center" prop="volumeRatio" />
      <el-table-column label="渠道" align="center" prop="channel" />
      <el-table-column label="总箱数" align="center" prop="totalBoxNum" />
      <el-table-column label="方数" align="center" prop="squareNum" />
      <el-table-column label="重量" align="center" prop="weight" />
      <el-table-column label="units" align="center" prop="units" />
      <el-table-column label="货值" align="center" prop="price" />
      <el-table-column label="仓库代号" align="center" prop="warehouseCode" />
      <el-table-column
        label="实际物流运费"
        align="center"
        prop="logisticsPrice"
      />
      <el-table-column label="平均单价" align="center" prop="aveUnitPrice" />
      <el-table-column
        label="开船日期"
        align="center"
        prop="sailDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sailDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="到港日期"
        align="center"
        prop="arrivalDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.arrivalDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="签收时间"
        align="center"
        prop="signForDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.signForDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="时效" align="center" prop="ageing" />
      <el-table-column label="运单号" align="center" prop="waybillNum" />
      <el-table-column
        label="付款状态 1-已付款 2-未付款"
        align="center"
        prop="paymentStatus"
      />
      <el-table-column label="船名" align="center" prop="shipsName" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="上架数量" align="center" prop="groundingNum" />
      <el-table-column
        label="删除状态 0-未删除 1-已删除"
        align="center"
        prop="isDelete"
      />
      <el-table-column
        label="状态 1-已完结 2-未完结"
        align="center"
        prop="status"
      />
      <el-table-column label="创建人ID" align="center" prop="createUserId" />
      <el-table-column
        label="创建人名称"
        align="center"
        prop="createUserName"
      />
      <el-table-column label="更新人ID" align="center" prop="updateUserId" />
      <el-table-column label="更新人ID" align="center" prop="updateUserName" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['logistics:logisticsTracking:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['logistics:logisticsTracking:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改物流追踪对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="Shipment ID" prop="shipmentId">
          <el-input v-model="form.shipmentId" placeholder="请输入Shipment ID" />
        </el-form-item>
        <el-form-item label="账号标记" prop="account">
          <el-input v-model="form.account" placeholder="请输入账号标记" />
        </el-form-item>
        <el-form-item label="发货日期" prop="sendDate">
          <el-date-picker
            clearable
            v-model="form.sendDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择发货日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="体积重除数" prop="volumeRatio">
          <el-input v-model="form.volumeRatio" placeholder="请输入体积重除数" />
        </el-form-item>
        <el-form-item label="渠道" prop="channel">
          <el-input v-model="form.channel" placeholder="请输入渠道" />
        </el-form-item>
        <el-form-item label="总箱数" prop="totalBoxNum">
          <el-input v-model="form.totalBoxNum" placeholder="请输入总箱数" />
        </el-form-item>
        <el-form-item label="方数" prop="squareNum">
          <el-input v-model="form.squareNum" placeholder="请输入方数" />
        </el-form-item>
        <el-form-item label="重量" prop="weight">
          <el-input v-model="form.weight" placeholder="请输入重量" />
        </el-form-item>
        <el-form-item label="units" prop="units">
          <el-input v-model="form.units" placeholder="请输入units" />
        </el-form-item>
        <el-form-item label="货值" prop="price">
          <el-input v-model="form.price" placeholder="请输入货值" />
        </el-form-item>
        <el-form-item label="仓库代号" prop="warehouseCode">
          <el-input v-model="form.warehouseCode" placeholder="请输入仓库代号" />
        </el-form-item>
        <el-form-item label="实际物流运费" prop="logisticsPrice">
          <el-input
            v-model="form.logisticsPrice"
            placeholder="请输入实际物流运费"
          />
        </el-form-item>
        <el-form-item label="平均单价" prop="aveUnitPrice">
          <el-input v-model="form.aveUnitPrice" placeholder="请输入平均单价" />
        </el-form-item>
        <el-form-item label="开船日期" prop="sailDate">
          <el-date-picker
            clearable
            v-model="form.sailDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择开船日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="到港日期" prop="arrivalDate">
          <el-date-picker
            clearable
            v-model="form.arrivalDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择到港日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="签收时间" prop="signForDate">
          <el-date-picker
            clearable
            v-model="form.signForDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择签收时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="时效" prop="ageing">
          <el-input v-model="form.ageing" placeholder="请输入时效" />
        </el-form-item>
        <el-form-item label="运单号" prop="waybillNum">
          <el-input v-model="form.waybillNum" placeholder="请输入运单号" />
        </el-form-item>
        <el-form-item label="船名" prop="shipsName">
          <el-input v-model="form.shipsName" placeholder="请输入船名" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="上架数量" prop="groundingNum">
          <el-input v-model="form.groundingNum" placeholder="请输入上架数量" />
        </el-form-item>
        <el-form-item label="删除状态 0-未删除 1-已删除" prop="isDelete">
          <el-input
            v-model="form.isDelete"
            placeholder="请输入删除状态 0-未删除 1-已删除"
          />
        </el-form-item>
        <el-form-item label="创建人ID" prop="createUserId">
          <el-input v-model="form.createUserId" placeholder="请输入创建人ID" />
        </el-form-item>
        <el-form-item label="创建人名称" prop="createUserName">
          <el-input
            v-model="form.createUserName"
            placeholder="请输入创建人名称"
          />
        </el-form-item>
        <el-form-item label="更新人ID" prop="updateUserId">
          <el-input v-model="form.updateUserId" placeholder="请输入更新人ID" />
        </el-form-item>
        <el-form-item label="更新人ID" prop="updateUserName">
          <el-input
            v-model="form.updateUserName"
            placeholder="请输入更新人ID"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <importExcel
      :open.sync="upload.open"
      :title="upload.title"
      :url="upload.url"
      :templateUrl="upload.templateUrl"
      :isUploading="upload.isUploading"
      @readList="getList"
    ></importExcel>
  </div>
</template>

<script>
import {
  listLogisticsTracking,
  getLogisticsTracking,
  delLogisticsTracking,
  addLogisticsTracking,
  updateLogisticsTracking,
} from "@/api/logistics/logisticsTracking";
import { getToken } from "@/utils/auth";
export default {
  name: "LogisticsTracking",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物流追踪表格数据
      logisticsTrackingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        shipmentId: null,
        account: null,
        sendDate: null,
        volumeRatio: null,
        channel: null,
        totalBoxNum: null,
        squareNum: null,
        weight: null,
        units: null,
        price: null,
        warehouseCode: null,
        logisticsPrice: null,
        aveUnitPrice: null,
        sailDate: null,
        arrivalDate: null,
        signForDate: null,
        ageing: null,
        waybillNum: null,
        paymentStatus: null,
        shipsName: null,
        groundingNum: null,
        isDelete: null,
        status: null,
        createUserId: null,
        createUserName: null,
        updateUserId: null,
        updateUserName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        sailDate: [
          { required: true, message: "开船日期不能为空", trigger: "blur" },
        ],
      },
      // 物流追踪导入参数
      upload: {
        // 是否显示弹出层（物流追踪导入）
        open: false,
        // 弹出层标题（物流追踪导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/logistics/logisticsTracking/importData",
        //导出模板地址
        templateUrl: "logistics/logisticsTracking/importTemplate",
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询物流追踪列表 */
    getList() {
      this.loading = true;
      listLogisticsTracking(this.queryParams).then((response) => {
        this.logisticsTrackingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        shipmentId: null,
        account: null,
        sendDate: null,
        volumeRatio: null,
        channel: null,
        totalBoxNum: null,
        squareNum: null,
        weight: null,
        units: null,
        price: null,
        warehouseCode: null,
        logisticsPrice: null,
        aveUnitPrice: null,
        sailDate: null,
        arrivalDate: null,
        signForDate: null,
        ageing: null,
        waybillNum: null,
        paymentStatus: null,
        shipsName: null,
        remark: null,
        groundingNum: null,
        createTime: null,
        isDelete: null,
        status: null,
        createUserId: null,
        createUserName: null,
        updateTime: null,
        updateUserId: null,
        updateUserName: null,
      };
      this.resetForm("form");
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "物流追踪导入";
      this.upload.open = true;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加物流追踪";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getLogisticsTracking(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改物流追踪";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateLogisticsTracking(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLogisticsTracking(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除物流追踪编号为"' + ids + '"的数据项？')
        .then(function () {
          return delLogisticsTracking(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "logistics/logisticsTracking/export",
        {
          ...this.queryParams,
        },
        `logisticsTracking_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
